﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
//for the RemotingConfiguration Class
using System.Runtime.Remoting.Channels;         //for the ChannelServices Class
using System.Runtime.Remoting.Channels.Tcp;     //for the TcpChannel Class
using System.Collections;
using GameInterface;
using System.Media;

namespace WindowsFormsApplication1
{
    public partial class Game1 : Form
    {
        #region Vars

        #region Remoting Vars

        /// <summary>
        /// the Server proxy.
        /// </summary>
        IGame ServerProxy;

        /// <summary>
        /// the string that will hold the IP address.
        /// </summary>
        private string IP;

        #endregion

        bool shot = false;
        /// <summary>
        /// true when it's the player's turn. false when it's the opponent's turn.
        /// </summary>
        bool MyTurn = false;

        /// <summary>
        /// true when the battle phase starts. before that false.
        /// </summary>
        bool sentRdy = false;

        /// <summary>
        /// bool if the sound is running or not.
        /// </summary>
        bool sndRunning;

        /// <summary>
        /// Game id of the current game instance
        /// </summary>
        int GID = -1;

        /// <summary>
        /// An instance of Login to be able to get back.
        /// </summary>
        Login log;

        /// <summary>
        /// player name
        /// </summary>
        string me;

        /// <summary>
        /// Ships1 = your ships.
        /// Ships2 = the enemies' ships.
        /// </summary>
        List<Ship> ShipsYou = new List<Ship>();
        List<Ship> ShipsOpp = new List<Ship>();
        
        /// <summary>
        /// List of shots.
        /// </summary>
        List<Shot> ShotsYou = new List<Shot>();
        List<Shot> ShotsOpp = new List<Shot>();

        /// <summary>
        /// Timer that will run the game.
        /// </summary>
        Timer Tmr;

        /// <summary>
        /// Graphics.
        /// </summary>
        private Graphics GmO;
        private Graphics GmY;
        private Graphics GgO;
        private Graphics GgY;

        /// <summary>
        /// This is the index of the selected ship in the list ShipsYou.
        /// </summary>
        private int selected = -1;

        /// <summary>
        /// redundant... int
        /// </summary>
        private int deadShip = -999;

        /// <summary>
        /// Image of the grid.
        /// </summary>
        private Image Grid = new Bitmap(Application.StartupPath + "\\Grid.png");

        /// <summary>
        /// these rectangles check if the ship doesn't cross the border when placed.
        /// </summary>
        private Rectangle borderX, borderY;

        #endregion
        #region Methods

        /// <summary>
        /// Constructor of the game.
        /// </summary>
        public Game1(string Ip, Login Log, int Sizer, string inviter, string invitee, string myName)
        {
            InitializeComponent();
            Visible = true;
            IP = Ip;
            log = Log;
            setupTcp();
            Connect();
            sizer(Sizer);
            creator(Sizer);
            Tmr = new Timer();
            Tmr.Tick += new EventHandler(Tmr_Tick);
            Tmr.Interval = 1;
            Tmr.Enabled = true;
            me = myName;
            Text = myName;
            sndRunning = Login.SoundOn;
            if (sndRunning)
            {
                Login.sound.PlayLooping();
            }
            else
            {
                soundCheck.Checked = false;
                Login.sound.Stop();
            }
            ServerProxy.event_send_GID += new SendGIDDelegate(ServerProxy_event_send_GID_handler);
            ServerProxy.event_send_msg += new SendChatDelegate(ServerProxy_event_getChat);
            ServerProxy.event_rdy_check += new SendRdyCheck(ServerProxy_event_rdy_handler);
            ServerProxy.event_ship_hit += new ShipHit(ServerProxy_shot_eventHandler);
            ServerProxy.event_send_starter += new SendStarter(ServerProxy_event_send_starter);
            ServerProxy.event_send_turn += new SendNextTurn(ServerProxy_event_send_turn);

            ServerProxy.newGame(Sizer, inviter, invitee);

            GmO = Graphics.FromImage(DrawMenuOpp.Image);
            GmY = Graphics.FromImage(DrawMenuYou.Image);
            GgO = Graphics.FromImage(DrawGameOpp.Image);
            GgY = Graphics.FromImage(DrawGameYou.Image);
        }

        /// <summary>
        /// adjusts the game form into the correct meassurements.
        /// </summary>
        private void sizer(int Sizer)
        {
            if (Sizer == 0)
            {
                ChatList.Size = new Size(226, 147);
                ChatList.Location = new Point(176, 2);
                txtSend.Location = new Point(219, 150);
                btnSend.Location = new Point(176, 150);
                DrawGameOpp.Size = new Size(81, 81);
                DrawGameOpp.Location = new Point(89, 2);
                DrawGameOpp.Image = new Bitmap(81, 81);
                DrawGameYou.Size = new Size(81, 81);
                DrawGameYou.Location = new Point(89, 89);
                DrawGameYou.Image = new Bitmap(81, 81);
                DrawMenuOpp.Size = new Size(81, 81);
                DrawMenuOpp.Location = new Point(2, 2);
                DrawMenuOpp.Image = new Bitmap(81, 81);
                DrawMenuYou.Size = new Size(81, 81);
                DrawMenuYou.Location = new Point(2, 89);
                DrawMenuYou.Image = new Bitmap(81, 81);
                DirectionCheck.Location = new Point(2, 177);
                soundCheck.Location = new Point(81, 177);
                btnTurn.Location = new Point(176, 173);
                btnSurrender.Location = new Point(309, 173);
                this.Size = new Size(420, 233);
                borderX = new Rectangle(81, 0, 1, 81);
                borderY = new Rectangle(0, 81, 81, 1);
            }
            else if (Sizer == 1)
            {
                ChatList.Size = new Size(226, 303);
                ChatList.Location = new Point(336, 2);
                txtSend.Location = new Point(379, 310);
                btnSend.Location = new Point(336, 310);
                DrawGameOpp.Size = new Size(161, 161);
                DrawGameOpp.Location = new Point(169, 2);
                DrawGameOpp.Image = new Bitmap(161, 161);
                DrawGameYou.Size = new Size(161, 161);
                DrawGameYou.Location = new Point(169, 169);
                DrawGameYou.Image = new Bitmap(161, 161);
                DrawMenuOpp.Size = new Size(161, 161);
                DrawMenuOpp.Location = new Point(2, 2);
                DrawMenuOpp.Image = new Bitmap(161, 161);
                DrawMenuYou.Size = new Size(161, 161);
                DrawMenuYou.Location = new Point(2, 169);
                DrawMenuYou.Image = new Bitmap(161, 161);
                DirectionCheck.Location = new Point(2, 337);
                soundCheck.Location = new Point(81, 337);
                btnTurn.Location = new Point(336, 333);
                btnSurrender.Location = new Point(469, 333);
                this.Size = new Size(580, 394);
                borderX = new Rectangle(161, 0, 1, 161);
                borderY = new Rectangle(0, 161, 161, 1);
            }
            else if (Sizer == 2)
            {
                ChatList.Size = new Size(226, 459);
                ChatList.Location = new Point(496, 2);
                txtSend.Location = new Point(539, 470);
                btnSend.Location = new Point(496, 470);
                DrawGameOpp.Size = new Size(241, 241);
                DrawGameOpp.Location = new Point(249, 2);
                DrawGameOpp.Image = new Bitmap(241, 241);
                DrawGameYou.Size = new Size(241, 241);
                DrawGameYou.Location = new Point(249, 249);
                DrawGameYou.Image = new Bitmap(241, 241);
                DrawMenuOpp.Size = new Size(241, 241);
                DrawMenuOpp.Location = new Point(2, 2);
                DrawMenuOpp.Image = new Bitmap(241, 241);
                DrawMenuYou.Size = new Size(241, 241);
                DrawMenuYou.Location = new Point(2, 249);
                DrawMenuYou.Image = new Bitmap(241, 241);
                DirectionCheck.Location = new Point(2, 497);
                soundCheck.Location = new Point(81, 497);
                btnTurn.Location = new Point(496, 493);
                btnSurrender.Location = new Point(629, 493);
                this.Size = new Size(743, 555);
                borderX = new Rectangle(241, 0, 1, 241);
                borderY = new Rectangle(0, 241, 241, 1);
            }
        }

        /// <summary>
        /// Creates the correct ships at the beginning of the game.
        /// </summary>
        private void creator(int Sizer)
        {
            if (Sizer == 0)
            {
                Ship s = new Ship(1, 0, 0);
                ShipsYou.Add(s);
                s = new Ship(1, 1000, 1000);
                s.dead = false;
                ShipsOpp.Add(s);
                s = new Ship(2, 0, 1);
                ShipsYou.Add(s);
                s = new Ship(2, 1000, 1000);
                s.dead = false;
                ShipsOpp.Add(s);
                s = new Ship(3, 0, 2);
                ShipsYou.Add(s);
                s = new Ship(3, 1000, 1000);
                s.dead = false;
                ShipsOpp.Add(s);
            }
            else if (Sizer == 1)
            {
                Ship s = new Ship(2, 0, 0);
                ShipsYou.Add(s);
                s = new Ship(2, 1000, 1000);
                s.dead = false;
                ShipsOpp.Add(s);
                s = new Ship(3, 0, 1);
                ShipsYou.Add(s);
                s = new Ship(3, 1000, 1000);
                s.dead = false;
                ShipsOpp.Add(s);
                s = new Ship(3, 0, 2);
                ShipsYou.Add(s);
                s = new Ship(3, 1000, 1000);
                s.dead = false;
                ShipsOpp.Add(s);
                s = new Ship(4, 0, 3);
                ShipsYou.Add(s);
                s = new Ship(4, 1000, 1000);
                s.dead = false;
                ShipsOpp.Add(s);
                s = new Ship(5, 0, 4);
                ShipsYou.Add(s);
                s = new Ship(5, 1000, 1000);
                s.dead = false;
                ShipsOpp.Add(s);
            }
            else if (Sizer == 2)
            {
                Ship s = new Ship(1, 0, 0);
                ShipsYou.Add(s);
                s = new Ship(1, 1000, 1000);
                s.dead = false;
                ShipsOpp.Add(s);
                s = new Ship(2, 0, 1);
                ShipsYou.Add(s);
                s = new Ship(2, 1000, 1000);
                s.dead = false;
                ShipsOpp.Add(s);
                s = new Ship(2, 0, 2);
                ShipsYou.Add(s);
                s = new Ship(2, 1000, 1000);
                s.dead = false;
                ShipsOpp.Add(s);
                s = new Ship(3, 0, 3);
                ShipsYou.Add(s);
                s = new Ship(3, 1000, 1000);
                s.dead = false;
                ShipsOpp.Add(s);
                s = new Ship(3, 0, 4);
                ShipsYou.Add(s);
                s = new Ship(3, 1000, 1000);
                s.dead = false;
                ShipsOpp.Add(s);
                s = new Ship(3, 0, 5);
                ShipsYou.Add(s);
                s = new Ship(3, 1000, 1000);
                s.dead = false;
                ShipsOpp.Add(s);
                s = new Ship(4, 0, 6);
                ShipsYou.Add(s);
                s = new Ship(4, 1000, 1000);
                s.dead = false;
                ShipsOpp.Add(s);
                s = new Ship(5, 0, 7);
                ShipsYou.Add(s);
                s = new Ship(5, 1000, 1000);
                s.dead = false;
                ShipsOpp.Add(s);
            }
        }

        /// <summary>
        /// Draws the game.
        /// </summary>
        private void Draw()
        {
            GmY.Clear(Color.LightBlue);
            GgY.Clear(Color.Transparent);
            GgY.DrawImage(Grid, 0, 0);

            GmO.Clear(Color.LightSalmon);
            GgO.Clear(Color.Transparent);
            GgO.DrawImage(Grid, 0, 0);

            DrawShipsOpp();
            DrawShipsYou();
            DrawShotsOpp();
            DrawShotsYou();

            DrawMenuOpp.Refresh();
            DrawGameOpp.Refresh();
            DrawMenuYou.Refresh();
            DrawGameYou.Refresh();
        }

        /// <summary>
        /// Draws all of the Opponent's ships.
        /// </summary>
        private void DrawShipsOpp()
        {
            for (int i = 0; i < ShipsOpp.Count; i++)
            {
                if (ShipsOpp[i].dead == true)
                {
                    SolidBrush brush = new SolidBrush(Color.DarkRed);
                    Rectangle R = new Rectangle(2, (i * 16 + 1), 16 * ShipsOpp[i].type, 15);
                    GmO.FillRectangle(brush, R);
                }
                else
                {
                    if (ShipsOpp[i].horizontal == true)
                    {
                        SolidBrush brush = new SolidBrush(Color.DarkRed);
                        Rectangle R = new Rectangle(((ShipsOpp[i].X) * 16 + 1), ((ShipsOpp[i].Y) * 16 + 1), 16 * ShipsOpp[i].type, 15);
                        GgO.FillRectangle(brush, R);
                        //Draw the ships in the field (horizontal)
                    }
                    else
                    {
                        SolidBrush brush = new SolidBrush(Color.DarkRed);
                        Rectangle R = new Rectangle(((ShipsOpp[i].X) * 16 + 1), ((ShipsOpp[i].Y) * 16 + 1), 15, 16 * ShipsOpp[i].type);
                        GgO.FillRectangle(brush, R);
                        //Draw the ships in the field (vertical)
                    }
                }
            }
        }

        /// <summary>
        /// Draws all of the Player's ships.
        /// </summary>
        private void DrawShipsYou()
        {

            for (int i = 0; i < ShipsYou.Count; i++)
            {
                if (ShipsYou[i].dead == true)
                {
                    SolidBrush brush = new SolidBrush(Color.DarkBlue);
                    GmY.FillRectangle(brush, ShipsYou[i].colBox);
                }
                else
                {
                    if (ShipsYou[i].horizontal == true)
                    {
                        SolidBrush brush = new SolidBrush(Color.DarkBlue);
                        GgY.FillRectangle(brush, ShipsYou[i].colBox);
                        //Draw the ships in the field (horizontal)
                    }
                    else
                    {
                        SolidBrush brush = new SolidBrush(Color.DarkBlue);
                        GgY.FillRectangle(brush, ShipsYou[i].colBox);
                        //Draw the ships in the field (vertical)
                    }
                }
            }
        }

        /// <summary>
        /// Draws all of the Opponent's shots..
        /// </summary>
        private void DrawShotsOpp()
        {
            for (int i = 0; i < ShotsOpp.Count; i++)
            {
                SolidBrush brush;
                if (ShotsOpp[i].hit == true)
                {
                    brush = new SolidBrush(Color.Red);
                }
                else
                {
                    brush = new SolidBrush(Color.Blue);
                }
                Rectangle R = new Rectangle((ShotsOpp[i].X * 16), (ShotsOpp[i].Y * 16), 15, 15);
                GgY.FillEllipse(brush, R);
            }
        }

        /// <summary>
        /// Draws all of the Player's shots.
        /// </summary>
        private void DrawShotsYou()
        {
            for (int i = 0; i < ShotsYou.Count; i++)
            {
                SolidBrush brush;
                if (ShotsYou[i].hit == true)
                {
                    brush = new SolidBrush(Color.Red);
                }
                else
                {
                    brush = new SolidBrush(Color.Blue);
                }
                Rectangle R = new Rectangle((ShotsYou[i].X * 16 + 1), (ShotsYou[i].Y * 16 + 1), 15, 15);
                GgO.FillEllipse(brush, R);
            }
        }

        /// <summary>
        /// the timer tick.
        /// </summary>
        private void Tmr_Tick(object sender, EventArgs e)
        {
            Draw();
            if (soundCheck.Checked && !sndRunning)
            {
                Login.SoundOn = true;
                Login.sound.PlayLooping();
                sndRunning = true;
            }
            else if (!soundCheck.Checked && sndRunning)
            {
                Login.SoundOn = false;
                Login.sound.Stop();
                sndRunning = false;
            }
        }

        #region Remoting Methods

        /// <summary>
        /// This sets up the TCP connection.
        /// </summary>
        static void setupTcp()
        {
            BinaryServerFormatterSinkProvider server_provider = new BinaryServerFormatterSinkProvider();
            server_provider.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;

            BinaryClientFormatterSinkProvider client_provider = new BinaryClientFormatterSinkProvider();

            IDictionary properties = new Hashtable();
            properties["port"] = 0;

            TcpChannel channel = new TcpChannel(properties, client_provider, server_provider);

            ChannelServices.RegisterChannel(channel, false);
        }

        /// <summary>
        /// To connect to the server.
        /// </summary>
        private void Connect()
        {
            string IPer;
            if (IP != "")
            {
                IPer = "tcp://";
                IPer += IP;
                IPer += ":7171/Server"; // check if this port is correct or 7171
                IP = IPer;
            }
            else
            {
                MessageBox.Show("The game failed to connect to the server, please try again!");
                // TODO: return to portal.
            }
            ServerProxy = (IGame)Activator.GetObject(typeof(IGame), IP);
            this.Text = me;
        }

        #endregion
        #region User Interface

        /// <summary>
        /// the surrender button.
        /// </summary>
        private void btnSurrender_Click(object sender, EventArgs e)
        {
            ServerProxy.Surrender(GID, me);
        }

        /// <summary>
        /// Menu click handling
        /// </summary>
        private void DrawMenuYou_MouseDown(object sender, MouseEventArgs e)
        {
            if (!sentRdy)
            {
                if (selected < 0)
                {
                    for (int i = 0; i < ShipsYou.Count; i++)
                    {
                        if (ShipsYou[i].colBox.IntersectsWith(new Rectangle(e.X, e.Y, 1, 1)) && ShipsYou[i].dead)
                        {
                            ShipsYou[i].X = 1000;
                            ShipsYou[i].Y = 1000;
                            ShipsYou[i].dead = false;
                            selected = i;
                            btnTurn.Enabled = false;
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Please place one ship at a time!");
                }
            }
        }

        /// <summary>
        /// Game click handling.
        /// </summary>
        private void DrawGameYou_MouseDown(object sender, MouseEventArgs e)
        {
            if (!sentRdy)
            {
                if (selected > -1)
                {
                    Rectangle R;
                    if (DirectionCheck.Checked)
                    {
                        ShipsYou[selected].horizontal = true;
                    }
                    else
                    {
                        ShipsYou[selected].horizontal = false;
                    }
                    if (ShipsYou[selected].horizontal)
                    {
                        R = new Rectangle((e.X / 16) * 16 + 1, (e.Y / 16) * 16 + 1, 16 * ShipsYou[selected].type - 1, 15);
                    }
                    else
                    {
                        R = new Rectangle((e.X / 16) * 16 + 1, (e.Y / 16) * 16 + 1, 15, 16 * ShipsYou[selected].type - 1);
                    }
                    if (selected > -1)
                    {
                        bool collision = false;
                        foreach (Ship s in ShipsYou)
                        {
                            if (s.colBox.IntersectsWith(R) && s != ShipsYou[selected] && !s.dead)
                            {
                                collision = true;
                                MessageBox.Show("Two ships are not able to be on to of each other.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                return;
                            }
                        }
                        if (R.IntersectsWith(borderX) || R.IntersectsWith(borderY))
                        {
                            collision = true;
                            MessageBox.Show("You may not place this ship outside the borders of the field.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                        if (!collision)
                        {
                            ShipsYou[selected].X = e.X / 16;
                            ShipsYou[selected].Y = e.Y / 16;
                            selected = -1;
                            bool Ready = true;
                            foreach (Ship s in ShipsYou)
                            {
                                if (s.dead)
                                {
                                    Ready = false;
                                }
                            }
                            if (Ready)
                            {
                                btnTurn.Enabled = true;
                            }
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < ShipsYou.Count; i++)
                    {
                        if (ShipsYou[i].colBox.IntersectsWith(new Rectangle(e.X, e.Y, 1, 1)) && !ShipsYou[i].dead)
                        {
                            ShipsYou[i].X = 1000;
                            ShipsYou[i].Y = 1000;
                            ShipsYou[i].dead = false;
                            selected = i;
                            btnTurn.Enabled = false;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// the send button click handling. (chat)
        /// </summary>
        private void btnSend_Click(object sender, EventArgs e)
        {
            string temp, user;
           
            temp = this.txtSend.Text.ToString();
            user = me;

            ServerProxy.chat(GID, temp, user);
            this.txtSend.Clear();
        }

        /// <summary>
        /// next turn/battlephase button click handling.
        /// </summary>
        private void btnTurn_Click(object sender, EventArgs e)
        {
            if (!sentRdy)
            {
                sentRdy = true;
                foreach (Ship s in this.ShipsYou)
                {
                    ServerProxy.set_ship(GID, s.type, s.X, s.Y, s.horizontal, me);
                }
                ServerProxy.playerRdy(GID, me);
                btnTurn.Enabled = false;
            }
        }

        void Game1_KeyPress(object sender, KeyPressEventArgs e)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Not hearing the error sound when press enter
        /// </summary>
        private void txtSend_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                e.Handled = true;
            }
        }

        /// <summary>
        /// shot fire handling.
        /// </summary>
        private void DrawGameOpp_MouseDown(object sender, MouseEventArgs e)
        {
            if (MyTurn)
            {
                Rectangle R = new Rectangle(e.X, e.Y, 1, 1);
                bool collision = false;
                foreach (Shot s in ShotsYou)
                {
                    if (s.colBox.IntersectsWith(R))
                    {
                        collision = true;
                        MessageBox.Show("You can't fire twice at the same spot.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                }
                if (!collision)
                {
                    int dead = -1;
                    ShotsYou.Add(new Shot(e.X / 16, e.Y / 16, false));
                    ServerProxy.checkShot(GID, (e.X / 16), (e.Y / 16), me, out dead);
                    shot = true;
                    btnTurn.Enabled = true;
                    MyTurn = false;
                    ServerProxy.endTurn(GID, me);
                }
            }
        }

        /// <summary>
        /// player press enter for chatting instead of click button send
        /// </summary>
        private void txtSend_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string temp, user;
                temp = this.txtSend.Text.ToString();
                user = me;
                ServerProxy.chat(GID, temp, user);
                this.txtSend.Clear();
            }
        }

        #endregion
        #region eventhandlers

        delegate void SetTextCallback(string text);

        /// <summary>
        /// eventhandler for setting each games GID
        /// </summary>
        public void ServerProxy_event_send_GID_handler(int gameID)
        {
            if (GID == -1)
            {
                GID = gameID;
            }
        }

        /// <summary>
        /// gets the chat messages
        /// </summary>
        public void ServerProxy_event_getChat(int gameID, string Message)
        {
            if (GID == gameID)
            {
                if (this.ChatList.InvokeRequired)
                {
                    // It's on a different thread, so use Invoke.
                    SetTextCallback d = new SetTextCallback(SetText);
                    this.BeginInvoke
                        (d, new object[] { Message });
                }
                else
                {
                    // It's on the same thread, no need for Invoke
                    ChatList.Items.Add(Message.ToString());
                } 
            }
        }

        /// <summary>
        /// sets the chat messages
        /// </summary>
        private void SetText(string text)
        {
            this.ChatList.Items.Add(text);
        }

        /// <summary>
        /// handles the event ready
        /// </summary>
        public void ServerProxy_event_rdy_handler(int gameID, string message)
        {
            if (GID == gameID)
            {
                if (!sentRdy)
                {
                    System.Windows.Forms.MessageBox.Show(message);
                }
            }
        }

        /// <summary>
        /// receives shots and puts then in the right list
        /// </summary>
        public void ServerProxy_shot_eventHandler(int gameID, bool hit, string player, int x, int y, int Dead)
        {
            if (GID == gameID)
            {
                if (player != me)
                {
                    if (Dead >= 0)
                    {
                        ShipsYou[Dead].dead = true;
                        ShipsYou[Dead].X = Dead;
                        ShipsYou[Dead].Y = 0;
                    }
                    ShotsOpp.Add(new Shot(x, y, hit));
                }
                else
                {
                    if (Dead >= 0)
                    {
                        ShipsOpp[Dead].dead = true;
                        ShipsOpp[Dead].X = Dead;
                        ShipsOpp[Dead].Y = 0;
                    }
                    ShotsYou[ShotsYou.Count - 1].hit = hit;
                }
            }
        }

        public void ServerProxy_event_send_starter(int GID, string starter)
        {
            if (me == starter)
            {
                MyTurn = true;                
            }
            else
            {
                MyTurn = false;
            }
            System.Windows.Forms.MessageBox.Show(starter + " is first to go!");
        }

        public void ServerProxy_event_send_turn(int GID, string nextTurn)
        {
            if (me != nextTurn)
            {
                MyTurn = true;
            }
            else
            {
                MyTurn = false;
            }
        }

        #endregion    
        
        #endregion
    }

    public class Shot
    {
        public int X;
        public int Y;
        public bool hit;
        public Rectangle colBox
        {
            get
            {
                return new Rectangle(((this.X) * 16 + 1), ((this.Y) * 16 + 1), 15, 15);
            }
        }

        public Shot(int x, int y, bool Hit)
        {
            X = x;
            Y = y;
            if (!Hit)
            {
                hit = false;
            }
            else
            {
                hit = true;
            }
        }
    }

    public class Ship
    {
        #region Variables

        public int type;
        public int X;
        public int Y;
        public bool horizontal = true;
        public bool dead = true;
        public Rectangle colBox
        {
            get
            {
                if (this.horizontal || this.dead)
                {
                    return new Rectangle(((this.X) * 16 + 1), ((this.Y) * 16 + 1), 16 * this.type - 1, 15);
                }
                else
                {
                    return new Rectangle(((this.X) * 16 + 1), ((this.Y) * 16 + 1), 15, 16 * this.type - 1);
                }
            }
        }

        #endregion
        #region Methods

        public Ship(int Typ, int x, int y)
        {
            type = Typ;
            X = x;
            Y = y;
        }

        #endregion
    }
}
